package org.pb.order.controller;

import org.pb.basic.utils.AjaxResult;
import org.pb.basic.utils.PageList;
import org.pb.order.domain.AdoptOrder;
import org.pb.order.query.AdoptOrderQuery;
import org.pb.order.service.IAdoptOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 收养订单的接口类
 * @author pb
 */
@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;

    /**
     * 领养订单提交
     * @param map 订单参数集合
     * @param request 订单请求对象
     * @return 返回订单处理结果
     */
    @PostMapping("/submit")
    public AjaxResult adoptSubmit(@RequestBody Map<String, Long> map, HttpServletRequest request) {
        try {
            // 订单提交处理
            adoptOrderService.submit(map, request);
            // 如果没有出现异常，则跳转到支付页面
            return AjaxResult.successWithMessage("订单创建成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 如果出现了异常，则将返回错误消息
            return AjaxResult.failedWithMessage(e.getMessage());
        }
    }

    /**
     * 管理员对应的领养订单查询接口
     * @param query 查询参数
     * @param request 查询请求对象
     * @return 返回查询结果
     */
    @PostMapping("/admin")
    public PageList<AdoptOrder> adminQuery(@RequestBody AdoptOrderQuery query, HttpServletRequest request) {
        // 执行查询
        return adoptOrderService.adminQuery(query, request);
    }

}
